package com.atues.kuangshengjuc.lock8;

import java.sql.Time;
import java.util.concurrent.TimeUnit;

public class Test2 {
    public static void main(String[] args) {
        Phone2 phone = new Phone2();
        new Thread(() -> {
            try {
                phone.sendMsg();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "A").start();
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        new Thread(() -> {
            try {
                phone.hello();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "B").start();

        new Thread(() -> {
            try {
                phone.call();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }, "C").start();

    }
}
//其实一开始可能进的就是sendMsg，但是由于停了4s钟，然后线程B获得了资源进行执行
//如果假如线程c那么线程C一定会在sendMsg()方法执行完毕后才能执行call()方法，因为锁的存在

class Phone2{

        //方法上锁的是对象锁
        public synchronized void sendMsg() throws Exception {
            System.out.println("ready to send msg");
            TimeUnit.SECONDS.sleep(4);
            System.out.println("发短信");
        }
        public synchronized void call() throws Exception {
            System.out.println("打电话");
        }
        public void hello() throws Exception {
            System.out.println("hello");
        }

}
